<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <style>
        div {
            color: deepskyblue;
        }
    </style>
</head>
<body>
<h1>微博首页</h1>
<div><!--v-show 满足条件时显示元素 不满足时隐藏-->
    <div v-show="user==''"><!--user没有值代表未登录-->
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <div v-show="user!=''">
        <p>欢迎{{user.nick}}回来!</p>
        <a href="/send.html">发布微博</a>
        <a href="my.html">我的微博</a>
        <a href="javascript:void(0)" @click="logout()">登出</a>
    </div>
    <hr>
    <div v-for="weibo in arr">
        <h3>
            <a :href="'/detail.html?id='+weibo.id">
                {{weibo.author}}说:{{weibo.content}}
            </a>
        </h3>
        <p>发布于:{{weibo.created}}</p>
        <a :href="'/detail.html?id='+weibo.id">
            <img :src="weibo.url" style="width:200px" alt="">
        </a>
        <hr>
    </div>
</div>
<!--引入vue和axios-->
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!--引入moment.js-->
<script src="http://cdn.staticfile.org/moment.js/2.24.0/moment.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            user: "",//代表当前登录的用户对象
            arr:[] //用来保存查询到的所有微博信息
        },
        created: function () {
            //发请求得到当前登录的用户信息
            axios.get("/currentUser").then(function (response) {
                v.user = response.data;
                console.log("user=" + v.user);
            });
            //发请求获取所有用户的微博数据
            axios.get("/weibo/select").then(function (response){
                v.arr = response.data;
                //遍历微博数据
                for (let weibo of v.arr) {
                    //修改时间格式
                    weibo.created = moment(weibo.created).format("YYYY年MM月DD日 HH:mm:ss")
                }
            })
        },
        methods: {
            logout: function () {
                if (confirm("您确定退出登录吗?")){
                    //发请求登出
                    axios.get("/logout").then(function (){
                        location.reload();
                    })
                }
            },
            moment:function (id){

            }
        }
    });
</script>
</body>
</html>